package com.lltt.study.controller;

import com.lltt.study.utils.DateTimeTools;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@Slf4j
public class NoToolCallingController {
    @Resource
    private ChatModel chatModel;
    @Resource
    private ChatClient chatClient;
    //v1 没有ToolCalling
    @GetMapping("/notoolcall/chat")
    public Flux<String>chat (@RequestParam(name = "msg",defaultValue = "你是谁，现在几点啦")String msg){
        return chatModel.stream(msg);
    }
    //v2
    @GetMapping("/toolcall/chat")
    public String chatTool (@RequestParam(name = "msg",defaultValue = "你是谁，现在几点啦")String msg){
        // 1.将工具注册到工具集
        ToolCallback[] tools = ToolCallbacks.from(new DateTimeTools());
        // 2.将工具集设置到 Chat 属性
        ToolCallingChatOptions options = ToolCallingChatOptions.builder()
                .toolCallbacks(tools)
                .build();
        // 3.构建提示词
        Prompt prompt = new Prompt(msg,options);
        // 4.调用大模型
        return chatModel.call(prompt).getResult().getOutput().toString();
    }
    @GetMapping("/toolcall/chat2")
    public Flux<String>chatTool2 (@RequestParam(name = "msg",defaultValue = "你是谁，现在几点啦")String msg){
        return chatClient.prompt(msg)
                .tools(new DateTimeTools())
                .stream()
                .content();
    }
}
